import Vue from 'vue';
import Vuex from 'vuex';
import {getCookie, setCookie} from '@/utils/cookiesUtil.js'
import { REVISE_INFO, LOGIN, LOGOUT, CHANGE_CITY, CHANGE_LOGO, SKIP_INDEX,
	REMOVE_ADDRESS, ADD_ADDRESS, ADD_SELECTED_ADDRESS, UPDATE_ADDRESS, SET_ADDRESS_LIST,
	SET_ORDER_INFO, DELETE_ORDER_INFO
	} from './mutation-types'
import userApis from '@/common/services/user.js'

Vue.use(Vuex);

// 把需要的个人信息先放出来～～
let userState = {
	nickname: getCookie('nickname'),
	headimgurl: getCookie('headimgurl'), 
	...uni.getStorageSync('userState')
};
// 用户所在的城市
let city = '';

// 收货地址需要 存储
let addressList =[];
// 用户选择的地址信息
let selectAddress = {};
// 用户查看的订单信息
let orderInfo = uni.getStorageSync('orderInfo') || [];
const store = new Vuex.Store({
    state: {userState, addressList, selectAddress, hasSkip: true, orderInfo, city},
	getters: {
		// 用户默认选择的地址
		defaultAddress: state => {
		  return state.addressList.find(todo => todo.isDefault) || state.addressList[state.addressList.length - 1]
		},
		orderInfos: state => {
		  return state.orderInfo
		},
		orderBackInfo: state => (id) => {
			console.log('orderBackInfo:', state.orderInfo.find(todo => {
				const orderId = todo.id || todo.orderId
				return orderId === id
			}));
			return state.orderInfo.find(todo => {
				const orderId = todo.id || todo.orderId
				return orderId === id
			})
		},
	},
    mutations: {
		// 关于用户登录
        [LOGIN](state, user) {  // 用户登录
            state.userState.nickname = user.nickname;
            state.userState.hasLogin = true;
			state.userState.headimgurl = user.headimgurl;
			state.userState.wechat = user.wechat;
			state.userState.qq = user.qq;
			state.userState.sex = user.sex;
			state.userState.userName = user.nickname;
			state.userState.birthday = user.birthday;
			state.userState.email = user.email;
			state.userState.mobilePhone = user.mobilePhone;
			state.userState.remmberme = user.remmberme;
			if (user.remmberme) {
				state.userState.password = user.password;
			} else {
				state.userState.password = '';
			}
			console.log('user.remmberme:', user, state.userState)
			uni.setStorageSync('userState', {...state.userState});	
        },
        [LOGOUT](state) {			// 用户退出
            state.userState.hasLogin = false;
			// todo: 当用户退出时，启动页可以不再显示
			uni.clearStorageSync('userState');
        },
		[CHANGE_CITY](state, city) {
			state.city = city;
			uni.setStorageSync('city', state.city);	
		},
		[CHANGE_LOGO](state, img){	// 用户修改图片
			state.headimgurl = img;	
			uni.setStorageSync('userState', {...state.userState});	
		},
		[SKIP_INDEX](state){	// 用户跳过首页
			state.hasSkip = true; 
			uni.setStorageSync('hasSkip', {...state.hasSkip});	
		},
		// 修改用户信息
		[REVISE_INFO](state , payload) {
			console.log('REVISE_INFO payload', payload)
			state.userState[payload.key] = payload.value;
			uni.setStorageSync('userState', {...state.userState});	
			let data = state.userState;
			delete data.hasLogin;
			delete data.hasSkip;
			userApis.setUserInfo(data).then(res => {
				console.log('统一处理用户数据修改:', res)
			})
		},
		// 关于地址管理
		[SET_ADDRESS_LIST](state , payload) {
			console.log('SET_ADDRESS_LIST payload', payload)
			state.addressList = payload.address
			uni.setStorageSync('addressList', {...state.addressList});	
		},
		[REMOVE_ADDRESS](state , payload) {
			console.log('REMOVE_ADDRESS payload', payload)
			const index = state.addressList.findIndex(item => item.id === payload.id)
			state.addressList.splice(index, 1)
			uni.setStorageSync('addressList', {...state.addressList});	
		},
		[ADD_ADDRESS](state , payload) {
			console.log('ADD_ADDRESS payload', payload)
			if (payload.address.isDefault) {
				state.addressList.find(item => item.isDefault).isDefault =false;
			}
			state.addressList.splice(state.addressList.length - 1, 0, payload.address)
			uni.setStorageSync('addressList', {...state.addressList});	
		},
		[UPDATE_ADDRESS](state , payload) {
			console.log('UPDATE_ADDRESS payload', payload)
			if (payload.address.isDefault) {
				state.addressList.find(item => item.isDefault).isDefault =false;
			}
			const index = state.addressList.findIndex(item => item.id === payload.id)
			state.addressList.splice(index, 1, payload.address)
			uni.setStorageSync('addressList', {...state.addressList});	
		},
		[ADD_SELECTED_ADDRESS](state , payload) {
		   console.log('ADD_SELECTED_ADDRESS payload', payload)
			state.selectAddress = payload.address
			uni.setStorageSync('addressList', {...state.selectAddress});	
		},
		[SET_ORDER_INFO](state , payload) {
			console.log('SET_ORDER_INFO payload', payload)
			state.orderInfo = payload.orderInfo;
			uni.setStorageSync('orderInfo', state.orderInfo);	
		},
		[DELETE_ORDER_INFO](state) {
			console.log('DELETE_ORDER_INFO payload', payload)
			state.orderInfo = [];
			uni.setStorageSync('orderInfo', state.orderInfo);	
		}
    },
	actions: {
		[REVISE_INFO]({ commit, state }, payload) {
			commit(REVISE_INFO, payload)
		},
		[REMOVE_ADDRESS]({ commit, state }, payload) {
			commit(REMOVE_ADDRESS, payload)
		},
		[SET_ADDRESS_LIST]({ commit, state }, payload) {
			commit(SET_ADDRESS_LIST, payload)
		},
		[UPDATE_ADDRESS]({ commit, state }, payload) {
			commit(UPDATE_ADDRESS, payload)
		},
		[ADD_ADDRESS]({ commit, state }, payload) {
			commit(ADD_ADDRESS, payload)
		},
		[ADD_SELECTED_ADDRESS]({ commit, state }, payload) {
			commit(ADD_SELECTED_ADDRESS, payload)
		},
		[SET_ORDER_INFO]({ commit, state }, payload) {
			commit(SET_ORDER_INFO, payload)
		},
		[DELETE_ORDER_INFO]({ commit, state }, payload) {
			commit(DELETE_ORDER_INFO, payload)
		}
	}
	
});

export default store
